package cn.inswf.display.movieclip {
	import flash.utils.getTimer;

	import cn.inswf.display.BitmapContainer;

	import flash.display.BitmapData;

	/**
	 * @author hi
	 */
	public class Bitmaps extends BitmapContainer {
		// 数据
		protected var _framelist : MovieClipFrameList;
		// 当前帧
		protected var _currentFrame : int = -1;
		// 总帧
		protected var _totalFrame : int = 0;
		// 总时间
		protected var _totalTime : Number = 0;
		// 正在播放
		protected var _playing : Boolean = true;
		// 更新位图
		protected var _draw : Boolean = true;
		// 时间速率
		protected var _timeScale : Number = 1;
		// 时度
		protected var _percent : Number = 0;
		// 是否反转播放
		protected var _reverse : Boolean;
		// 是否最后一帧
		protected var _end : Boolean;
		protected var _nextTime : Number = 0;

		public function Bitmaps(bitmapData : BitmapData = null, pixelSnapping : String = "auto", smoothing : Boolean = false) {
			super(bitmapData, pixelSnapping, smoothing);
		}

		public function reset() : void {
			// 重置
			_end = false;
			_draw = true;
			reverse = false;
			timeScale = 1;
			playing = true;
		}

		public function setFrameData(value : MovieClipFrameList) : void {
			if (_framelist == value) return;
			if (value == null) {
				playing = false;
				_draw = false;
			}
			_framelist = value.clone();
			_end = false;
			_percent = 0;
			_currentFrame = -1;
			if (_reverse) {
				_framelist.reverse();
			}
			_totalTime = _framelist.totalTime;
			_totalFrame = _framelist.length;
			_framelist.timeScale = _timeScale;
			_nextTime = _framelist.getFrameTime(0) + getTimer();
			if (_totalFrame < 2) {
				from(_framelist.getFrame(0));
			}
			progress(0);
		}

		public function get currentFrame() : int {
			return _currentFrame;
		}

		public function get totalFrame() : int {
			return _totalFrame;
		}

		override public function update(time : Number) : void {
			if (!_renderOnRemove && stage == null) return;
			if (!_playing) return;
			if (_framelist == null) {
				super.update(time);
				return;
			}
			if (_totalFrame < 2) {
				super.update(time);
				return;
			}
			if (time > _nextTime) {
				var cf : int = _framelist.getTimeIndex(time);
				var percent : Number = _framelist.percent;
				if (percent < _percent) {
					_end = true;
					progress(1);
				} else {
					if (_end) {
						_end = false;
						progress(0);
					}
					progress(percent);
				}
				_percent = percent;
				if (cf != _currentFrame) {
					frameChange(cf, _currentFrame);
					_currentFrame = cf;
					if (_draw) {
						from(_framelist.getFrame(_currentFrame));
					}
					_nextTime = _framelist.getFrameTime(_currentFrame) + time;
				}
			}
			super.update(time);
		}

		public function get playing() : Boolean {
			return _playing;
		}

		protected function frameChange(current : int, old : int) : void {
		}

		protected function progress(value : Number) : void {
		}

		public function set reverse(value : Boolean) : void {
			if (value == _reverse) return;
			_reverse = value;
			if (_framelist) {
				_framelist.reverse();
			}
		}

		public function set timeScale(value : Number) : void {
			_timeScale = value;
			if (_framelist) {
				_framelist.timeScale = _timeScale;
			}
		}

		public function get timeScale() : Number {
			return _timeScale;
		}

		public function set playing(value : Boolean) : void {
			if (_playing == value) return;
			_playing = value;
			if (_playing) {
				_framelist.percent = _percent;
			}
		}

		public function set currentFrame(value : int) : void {
			if (_framelist == null) return;
			if (value < 0) value = 0;
			if (value > _totalFrame) value = _totalFrame;
			_currentFrame = value;
			_framelist.gotoFrame(_currentFrame);
			from(_framelist.getFrame(_currentFrame));
		}
	}
}
